home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 102_01.zip / PACMONST.C < prev    next >
Text File  |  1993-06-03  |  6KB  |  299 lines

  1. #include    "pacdefs.h"
  2.  
  3. startmonst()
  4. {
  5.     struct pac *mptr;
  6.     int monstnum;
  7.  
  8.     if (potion == TRUE)
  9.     {
  10.         /* don't start if potion active */
  11.         return;
  12.     };
  13.  
  14.     for (mptr = &monst[0], monstnum = 0; monstnum < MAXMONSTER; mptr++, monstnum++)
  15.     {
  16.         if (mptr->stat == START)
  17.         {
  18.             rscore[monstnum] = 1;
  19.  
  20.             /* clear home */
  21.             PLOT(mptr->ypos, mptr->xpos, VACANT);
  22.  
  23.             /* initialize moving monster */
  24.             mptr->ypos = MBEGINY;
  25.             mptr->xpos = MBEGINX;
  26.             mptr->speed = SLOW;
  27.             mptr->danger = TRUE;
  28.             mptr->stat = RUN;
  29.             PLOT(MBEGINY, MBEGINX, MONSTER);
  30.  
  31.             /* DRIGHT or DLEFT? */
  32.             mptr->dirn = getrand(2) + DLEFT;
  33.             break;
  34.         };
  35.     };
  36. }
  37.  
  38. monster(mnum)
  39.     int mnum;
  40. {
  41.     int newx,newy;
  42.     int tmpx, tmpy;
  43.     struct pac *mptr;
  44.     int blinking;
  45.  
  46.     mptr = &monst[mnum];
  47.  
  48.     /* remember monster's current position */
  49.     tmpx = mptr->xpos;
  50.     tmpy = mptr->ypos;
  51.  
  52.     /* if we can, let's move a monster */
  53.     if (mptr->stat == RUN)
  54.     {
  55.         blinking = 1;        /* blinking monster?    */
  56.         /* if a monster was displayed ... */
  57.  
  58.         if ((blinking == 1) ||
  59.             ((blinking == 0) &&
  60.             (( (rounds - 1) % rscore[mnum]) == 0)))
  61.         {
  62.             /* replace display character */
  63.             PLOT(tmpy, tmpx, display[tmpy][tmpx]);
  64.         };
  65.  
  66.         /* get a new direction */
  67.         mptr->dirn = which(mptr, tmpx, tmpy);
  68.         switch (mptr->dirn)
  69.         {
  70.         case DUP:
  71.             newy = tmpy + UPINT;
  72.             newx = tmpx;
  73.             break;
  74.  
  75.         case DDOWN:
  76.             newy = tmpy + DOWNINT;
  77.             newx = tmpx;
  78.             break;
  79.  
  80.         case DLEFT:
  81.             newx = tmpx + LEFTINT;
  82.             newy = tmpy;
  83.             if (newx <= 0)
  84.                 newx = XWRAP;    /* wrap around */
  85.             break;
  86.  
  87.         case DRIGHT:
  88.             newx = tmpx + RIGHTINT;
  89.             newy = tmpy;
  90.             if (newx >= XWRAP)
  91.                 newx = 0;    /* wrap around */
  92.             break;
  93.         }
  94.  
  95.         /* use brd to determine if this was a valid direction */
  96.         switch (brd[newy][newx])
  97.         {
  98.         case GOLD:
  99.         case VACANT:
  100.         case POTION:
  101.         case TREASURE:
  102.         case CHOICE:
  103.             /* set new position */
  104.             mptr->xpos = newx;
  105.             mptr->ypos = newy;
  106.  
  107.             /* run into a pacman? */
  108.             if ((newy == pacptr->ypos) &&
  109.                 (newx == pacptr->xpos))
  110.             {
  111.                 killflg = dokill(mnum);
  112.             };
  113.             rscore[mnum] = pscore / 100 + 1;
  114.             if ((blinking == 1) || (killflg == TURKEY) ||
  115.                 ( (blinking == 0) &&
  116.                 ((rounds % rscore[mnum]) == 0)))
  117.             {
  118.  
  119.                 if (mptr->danger == TRUE)
  120.                 {
  121.                     PLOT(newy, newx, MONSTER);
  122.                 }
  123.                 else if (killflg != GOTONE)
  124.                 {
  125.                     PLOT(newy, newx, RUNNER);
  126.                 };
  127.             };
  128.             break;
  129.  
  130.         default:
  131.             errgen("bad direction");
  132.             break;
  133.         };
  134.     }
  135. }
  136.  
  137. which(mptr, x, y)    /* which directions are available ? */
  138.     struct pac *mptr;
  139.     int x, y;
  140. {
  141.     int movecnt;
  142.     int submovecnt;
  143.     int next;
  144.     int moves[4];
  145.     int submoves[4];
  146.     int nydirn, nxdirn;
  147.     int goodmoves;
  148.     int offx, offy;
  149.     int tmpdirn;
  150.     char *brdptr;
  151.  
  152.     /*
  153.      * As a general rule: determine the set of all
  154.      * possible moves, but select only those moves
  155.      * that don't require a monster to backtrack.
  156.      */
  157.     movecnt = 0;
  158.     brdptr = &(brd[y][x]);
  159.     if (((tmpdirn = mptr->dirn) != DDOWN) &&
  160.         ((next = *(brdptr + (BRDX * UPINT))) != WALL) &&
  161.         (next != GATE))
  162.     {
  163.         moves[movecnt++] = DUP;
  164.     };
  165.     if ((tmpdirn != DUP) &&
  166.         ((next = *(brdptr + (BRDX * DOWNINT))) != WALL) &&
  167.         (next != GATE))
  168.     {
  169.         moves[movecnt++] = DDOWN;
  170.     };
  171.     if ((tmpdirn != DRIGHT) &&
  172.         ((next = *(brdptr + LEFTINT)) != WALL) &&
  173.         (next != GATE))
  174.     {
  175.         moves[movecnt++] = DLEFT;
  176.     };
  177.     if ((tmpdirn != DLEFT) &&
  178.         ((next = *(brdptr + RIGHTINT)) != WALL) &&
  179.         (next != GATE))
  180.     {
  181.         moves[movecnt++] = DRIGHT;
  182.     };
  183.  
  184.     /*
  185.      * If player is scoring high, make monsters intelligent (SAW)
  186.      */
  187.  
  188.     if (getrand(MINTEL) < pscore)
  189.     {
  190.         /* make monsters intelligent */
  191.         if (pacptr->danger == TRUE)
  192.         {
  193.             /*
  194.              * Holy Cow!! The pacman is dangerous,
  195.              * permit monsters to reverse direction
  196.              */
  197.             switch (tmpdirn)
  198.             {
  199.             case DUP:
  200.                 if ((*(brdptr + (BRDX * DOWNINT)) != WALL) &&
  201.                     (*(brdptr + (BRDX * DOWNINT)) != GATE))
  202.                 {
  203.                     moves[movecnt++] = DDOWN;
  204.                 };
  205.                 break;
  206.  
  207.             case DDOWN:
  208.                 if ((*(brdptr + (BRDX * UPINT)) != WALL) &&
  209.                     (*(brdptr + (BRDX * UPINT)) != GATE))
  210.                 {
  211.                     moves[movecnt++] = DUP;
  212.                 };
  213.                 break;
  214.  
  215.             case DRIGHT:
  216.                 if ((*(brdptr + LEFTINT) != WALL) &&
  217.                     (*(brdptr + LEFTINT) != GATE))
  218.                 {
  219.                     moves[movecnt++] = DLEFT;
  220.                 };
  221.                 break;
  222.  
  223.             case DLEFT:
  224.                 if ((*(brdptr + RIGHTINT) != WALL) &&
  225.                     (*(brdptr + RIGHTINT) != GATE))
  226.                 {
  227.                     moves[movecnt++] = DRIGHT;
  228.                 };
  229.                 break;
  230.             };
  231.         };
  232.  
  233.         /* determine the offset from the pacman */
  234.         offx = x - pacptr->xpos;
  235.         offy = y - pacptr->ypos;
  236.         if (offx > 0)
  237.         {
  238.             /*need to go left */
  239.             nxdirn = DLEFT;
  240.         }
  241.         else
  242.         {
  243.             if (offx < 0)
  244.             {
  245.                 nxdirn = DRIGHT;
  246.             }
  247.             else
  248.             {
  249.                 /*need to stay here */
  250.                 nxdirn = DNULL;
  251.             };
  252.         };
  253.         if (offy > 0)
  254.         {
  255.             /*need to go up */
  256.             nydirn = DUP;
  257.         }
  258.         else
  259.         {
  260.             if (offy < 0)
  261.             {
  262.                 /* need to go down */
  263.                 nydirn = DDOWN;
  264.             }
  265.             else
  266.             {
  267.                 /* need to stay here */
  268.                 nydirn = DNULL;
  269.             };
  270.         };
  271.         goodmoves = 0;
  272.         for (submovecnt = 0; submovecnt < movecnt; submovecnt++)
  273.         {
  274.             if (pacptr->danger == FALSE)
  275.             {
  276.                 if ((moves[submovecnt] == nydirn) ||
  277.                     (moves[submovecnt] == nxdirn))
  278.                 {
  279.                     submoves[goodmoves++] = moves[submovecnt];
  280.                 };
  281.             }
  282.             else
  283.             {
  284.                 if ((moves[submovecnt] != nydirn) &&
  285.                     (moves[submovecnt] != nxdirn))
  286.                 {
  287.                     submoves[goodmoves++] = moves[submovecnt];
  288.                 };
  289.             };
  290.         };
  291.         if (goodmoves > 0)
  292.         {
  293.             return(submoves[getrand(goodmoves)]);
  294.         };
  295.     };
  296.     return(moves[getrand(movecnt)]);
  297. }
  298.  
  299.